package com.power200.android.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.power200.android.R;
import com.power200.android.bean.BackLog;
import com.power200.android.service.BackLogService;
import com.power200.android.service.ID;

import org.w3c.dom.Text;

public class BacklogCreate extends AppCompatActivity {

    private BackLogService backLogService = BackLogService.getInstance();

    private Spinner prioritySpinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backlog_create);

        prioritySpinner = (Spinner) findViewById(R.id.backlog_create_priority);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.priorities, android.R.layout.simple_spinner_dropdown_item);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        prioritySpinner.setAdapter(adapter);

        Button saveButton = (Button) findViewById(R.id.saveBacklogButton);
        saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText nameText = (EditText)findViewById(R.id.backlog_create_name);
                EditText briefText = (EditText)findViewById(R.id.backlog_create_brief);
                EditText descriptionText = (EditText)findViewById(R.id.backlog_create_description);
                String priority = prioritySpinner.getSelectedItem().toString();

                BackLog bean = new BackLog();
                bean.setId(ID.get());
                bean.setPriority(Integer.parseInt(priority));
                bean.setName(((TextView) nameText).getText().toString());
                bean.setBrief(((TextView) briefText).getText().toString());
                bean.setDescription(((TextView) descriptionText).getText().toString());
                backLogService.createBackLog(bean);

                Intent intent = new Intent(BacklogCreate.this,Backlog.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_backlog_create, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
